package com.banciyuan.wallpaper.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.banciyuan.wallpaper.setting.SettingConfig;
import com.banciyuan.wallpaper.util.NetUtils;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

/**
 * Created by squall on 2016/8/1.
 */
public class ListenSevices extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 24);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (SettingConfig.ReadSettingBoolean(getBaseContext(), SettingConfig.CHANGE)) {
                    if (SettingConfig.ReadSettingBoolean(getBaseContext(), SettingConfig.WIFI)) {
                        if (NetUtils.isWifi(getBaseContext())) {
                            Intent sIntent = new Intent(getBaseContext(), ChangeSevices.class);
                            startService(sIntent);
                        }
                    } else {
                        Intent sIntent = new Intent(getBaseContext(), ChangeSevices.class);
                        startService(sIntent);
                    }
                }
            }
        }, today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
//        }, SystemClock.currentThreadTimeMillis(), 10000);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent();
        intent.setAction("change.restart");
        getBaseContext().sendBroadcast(intent);
    }
}
